package com.example.yls.demoa;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private Button btnDB;
    private Button btnStuTable;
    private Button btnInsertStudb;
    private Button btnSearhAllStudb;
    private TextView textData;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnDB = findViewById(R.id.btn_db);
        btnDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建数据库
                createDatabase();
            }
        });

        btnStuTable = findViewById(R.id.btn_dbStu);
        btnStuTable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //创建学生表
                createStuTable();
            }
        });

        btnInsertStudb = findViewById(R.id.btn_insertStudb);
        btnInsertStudb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //插入学生数据
                insertIntoable();
            }
        });


        textData = findViewById(R.id.text1);
        btnSearhAllStudb = findViewById(R.id.btn_searchAllStudb);
        btnSearhAllStudb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //查询表的所有数据
                searchAllStuData();
            }
        });
    }


    private void createDatabase() {
        String path = getFilesDir().getAbsolutePath() + File.separator + "stu.db";
        Log.e("MainActivity", "path = " + path);
        db = SQLiteDatabase.openOrCreateDatabase(path, null);
    }


    private void createStuTable() {
        if (db == null) {
            createDatabase();
        }
        String sql = "create table if not exists Student (stuno int, name varchar(10), age int)";
        db.execSQL(sql);
    }


    private void insertIntoable() {
        if (db == null) {
            createDatabase();
        }

        // 第一种
        /*String sql = "insert into Student values(10086,'渣渣辉',37)";
        db.execSQL(sql);*/
        // 第二种
        ContentValues values = new ContentValues();
        values.put("stuno",10081);
        values.put("name","古天乐");
        values.put("age",38);
        db.insert("Student",null,values);

    }


    private void searchAllStuData() {
        if (db == null){
            createDatabase();
        }

        Cursor cursor = db.query("Student",null,
                null,null,null,
                null,null,null);

        if (cursor == null || cursor.getCount() == 0){
            System.out.println("数据库没有这张表");
            return;
        }

        StringBuffer buffer = new StringBuffer();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
            int stuno = cursor.getInt(0);
            String name = cursor.getString(1);
            int age = cursor.getInt(2);
            Log.e("searchAllStuData","stuno =" + stuno + "name =" + name + "age =" +age);
            buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");

        }
        textData.setText(buffer);

    }

}
